package com.kcl.dfss.xcamera;

import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;

import android.graphics.Bitmap;

public class XCAlgoTrue implements XCAlgo {

	private static XCAlgoTrue singletInstance = null;
	private static XCamAlgoResult adasResult = new XCamAlgoResult();
	private Mat colorImg,grayImg;
	private static int crossX = 325;
	private static int crossY = 251;
	
	private XCAlgoTrue(){
		
	}
	
	public static XCAlgoTrue getInstance() {
		if(singletInstance == null){
			singletInstance = new XCAlgoTrue();
		}
		return singletInstance;
	}

	@Override
	public XCAlgoTrue getInstanceWithName(String algoName) {
		
		return XCAlgoTrue.getInstance();
	}

	@Override
	public boolean initAdaswithCamera(XCCameraParam camera) {
		
		return true;
	}

	@Override
	public boolean setCarWidth(float width) {
		
		return true;
	}

	@Override
	public boolean startAutoCalib() {
		
		return true;
	}

	@Override
	public boolean resetCalibEofX(float px, float py) {
		crossX = (int) Math.max(Math.min(px, 640), 0);
		crossY = (int) Math.max(Math.min(py, 640), 0);
		return true;
	}

	@Override
	public float getPitch() {
		
		return 0;
	}
	
	@Override
	public float getYaw() {
		
		return 0;
	}

	@Override
	public XCamAlgoResult processImage(char image, XCHostParam host) {
		
		long ldwStateCounter = 0;
		long fcwStateCounter = 0;
		long lkaStateCounter = 0;
		
		if(image == '\0'){	//此处有疑问
			return null;
		}
		
		adasResult.cross[0] = crossX;//软件标记界面两个线交点里的 x 和 y
		adasResult.cross[1] = crossY;
		
		if(ldwStateCounter<1000){
			adasResult.lane_active[0] = 1;//左侧 线是否检测到  空闲表示
			adasResult.lane_active[1] = 0;//右侧
			adasResult.lane_warning_flag[0] = 0;//0 是safe 白色  1 是偏离 但不危险 对应黄色 2 是无意识的偏离 对应红色。
			adasResult.lane_warning_flag[1] = 0;//
		}else if(ldwStateCounter<200){
			adasResult.lane_active[0] = 1;
	        adasResult.lane_active[1] = 0;
	        adasResult.lane_warning_flag[0] = 0;
	        adasResult.lane_warning_flag[1] = 0;
		}else if (ldwStateCounter < 300){
	        adasResult.lane_active[0] = 1;
	        adasResult.lane_active[1] = 1;
	        adasResult.lane_warning_flag[0] = 1;
	        adasResult.lane_warning_flag[1] = 0;
		}else if (ldwStateCounter < 400){
	        adasResult.lane_active[0] = 1;
	        adasResult.lane_active[1] = 1;
	        adasResult.lane_warning_flag[0] = 0;
	        adasResult.lane_warning_flag[1] = 1;
	    }else if (ldwStateCounter < 500){
	        adasResult.lane_active[0] = 1;
	        adasResult.lane_active[1] = 1;
	        adasResult.lane_warning_flag[0] = 2;
	        adasResult.lane_warning_flag[1] = 0;
	    }else if (ldwStateCounter < 600){
	        adasResult.lane_active[0] = 1;
	        adasResult.lane_active[1] = 1;
	        adasResult.lane_warning_flag[0] = 0;
	        adasResult.lane_warning_flag[1] = 2;
	    }
		//1 -1 0 
		adasResult.lane_type[0] = 1;
		adasResult.lane_type[1] = 2;
		ldwStateCounter = (ldwStateCounter == 700)?0:(ldwStateCounter+1);
		adasResult.lkp_count = lkaStateCounter/20;
		lkaStateCounter = (lkaStateCounter == 20*60)?0:(lkaStateCounter+1);
		
		if(fcwStateCounter>=100){
			adasResult.have_egolane_vehicle = true;
			adasResult.fcw_warning_level = 0;
			adasResult.fcw_ttc_confidence = 0.9f;
			adasResult.fcw_ttc = 10.0f;
			adasResult.fcw_distance = 100 - fcwStateCounter/10;
			if(fcwStateCounter>=200 && fcwStateCounter<400){
				adasResult.fcw_warning_level = 1;
				adasResult.fcw_ttc = 5.0f;
			}else if(fcwStateCounter>=400){
				adasResult.fcw_warning_level = 2;
				adasResult.fcw_ttc = 5.0f - (fcwStateCounter-400)/65.0f;
			}
		}else{
			adasResult.have_egolane_vehicle = false;
			adasResult.fcw_warning_level = 0;
			adasResult.fcw_ttc = -1;
			adasResult.fcw_ttc_confidence = 0;
			adasResult.fcw_distance = 0;
		}
		
		fcwStateCounter = (fcwStateCounter == 60*10)?0:(fcwStateCounter+1);
		
		return adasResult;
	}

	
	@Override
	public XCamAlgoResult processUIImage(Bitmap bitmap, XCHostParam host) {
		if(bitmap == null){
			colorImg = this.cvMatFromUIImage(bitmap);
			if(!colorImg.empty()){
				Imgproc.cvtColor(colorImg, grayImg, Imgproc.COLOR_RGB2GRAY);
//				return this.processImage(grayImg.da, host);
				return null;
			}else{
				return null;
			}
		}else{
			return null;
		}
	}

	@Override
	public Bitmap getProcessedUIImage() {
		this.drawImge(colorImg);
		return this.UIImageFromCVMat(colorImg);
	}
	
	/*
	 * 未完待续
	 */
	
	public Mat cvMatFromUIImage(Bitmap bitmap){
		
		return null;
	}
	
	public Mat cvMatGrayFromUIImage(Bitmap bitmap){
		
		return null;
	}

	public Bitmap UIImageFromCVMat(Mat cvMat){
		
		return null;
	}
	
	public void drawImge(Mat image){
		
	}
	
	
}
